﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace EdexApiClient
{
    public partial class EdexApiContext
    {
        /// <summary>
        /// Получить перечень компаний
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        public IEnumerable<WH_COMPANIES> GetSuppliers()
        {
            return this.GetSuppliers(String.Empty);
        }

        /// <summary>
        /// Получить перечень компаний
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        public IEnumerable<WH_COMPANIES> GetSuppliers(string search)
        {
            var uri = String.Format("Suppliers/?api_key={0}", this.session.APIKEY);

            if (!String.IsNullOrEmpty(search))
                uri += "&$filter=substringof('" + search + "', COMPANY_NAME) eq true&$top=100";

            uri += "&$orderby=COMPANY_NAME";

            return this.GetData<List<WH_COMPANIES>>(uri);
        }

        public WH_COMPANIES GetSupplierDetails(Guid id)
        {
            var uri = String.Format("Suppliers/{0}/?api_key={1}", id.ToString(), this.session.APIKEY);
            return this.GetData<WH_COMPANIES>(uri);
        }

        public void AddSupplier(WH_COMPANIES data)
        {
            var uri = String.Format("Suppliers/?api_key={0}", this.session.APIKEY);
            this.Post<WH_COMPANIES>(uri, data);
        }

        public void UpdateSupplier(WH_COMPANIES data)
        {
            var uri = String.Format("Suppliers/?api_key={0}", this.session.APIKEY);
            this.Put<WH_COMPANIES>(uri, data);
        }

        public void DeleteSupplier(string id)
        {
            var uri = String.Format("Suppliers/{0}/?api_key={1}", id, this.session.APIKEY);
            this.Delete(uri);
        }
    }
}